using Kickball.Step1;
using Kickball.Step2;
using Kickball.Step3;
using Unity.Burst;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Transforms;
using UnityEngine;

namespace Kickball.Step5
{
    public partial struct BallCarrySystem : ISystem
    {
        /// <summary>
        /// 持球时，球相对于球员的位置偏移量
        /// </summary>
        static readonly float3 CarryOffset = new float3(0, 2, 0);

        [BurstCompile]
        public void OnCreate(ref SystemState state)
        {
            state.RequireForUpdate<Execute.BallCarry>();
            state.RequireForUpdate<Config>();
        }

        [BurstCompile]
        public void OnUpdate(ref SystemState state)
        {
            var config = SystemAPI.GetSingleton<Config>();

            //带球移动
            foreach (var (ballTrans, carrier) in SystemAPI.Query<RefRW<LocalTransform>, RefRO<Carry>>().WithAll<Ball>())
            {
                //获取持球球员
                var playerTrans = state.EntityManager.GetComponentData<LocalTransform>(carrier.ValueRO.Target);

                //更新球的位置
                ballTrans.ValueRW.Position = playerTrans.Position + CarryOffset;
            }

            if (!Input.GetKeyDown(KeyCode.C)) return;

            foreach (var (playerTrans, playerEntity) in SystemAPI.Query<RefRW<LocalTransform>>()
                         .WithAll<Player>()
                         .WithEntityAccess())
            {
                if (state.EntityManager.IsComponentEnabled<Carry>(playerEntity)) //球员持球
                {
                    //放下球
                    var carried = state.EntityManager.GetComponentData<Carry>(playerEntity); //获取被持有的球的实体

                    //设置球的位置
                    var ballTrans = state.EntityManager.GetComponentData<LocalTransform>(carried.Target);
                    ballTrans.Position = playerTrans.ValueRO.Position;
                    state.EntityManager.SetComponentData(carried.Target, ballTrans);

                    //标记持球状态
                    state.EntityManager.SetComponentEnabled<Carry>(playerEntity, false);
                    state.EntityManager.SetComponentEnabled<Carry>(carried.Target, false);

                    //解除球和球员的双向绑定
                    state.EntityManager.SetComponentData(playerEntity, new Carry());
                    state.EntityManager.SetComponentData(carried.Target, new Carry());
                }
                else //球员未持球
                {
                    //持有指定区域内的首个球
                    foreach (var (ballTrans, ballEntity) in SystemAPI.Query<RefRO<LocalTransform>>()
                                 .WithAll<Ball>()
                                 .WithDisabled<Carry>()
                                 .WithEntityAccess())
                    {
                        float distSq = math.distancesq(playerTrans.ValueRO.Position, ballTrans.ValueRO.Position);
                        if (distSq < config.BallKickingRangeSq) //在可踢球范围内
                        {
                            //球速度清零
                            state.EntityManager.SetComponentData(ballEntity, new Velocity());

                            //球和球员双向绑定
                            state.EntityManager.SetComponentData(playerEntity, new Carry()
                            {
                                Target = ballEntity,
                            });
                            state.EntityManager.SetComponentData(ballEntity, new Carry()
                            {
                                Target = playerEntity,
                            });

                            //标记持球状态
                            state.EntityManager.SetComponentEnabled<Carry>(playerEntity, true);
                            state.EntityManager.SetComponentEnabled<Carry>(ballEntity, true);

                            break;
                        }
                    }
                }
            }
        }
    }
}